package com.cjk.console.ctrl;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjk.console.SessionApi;
import com.cjk.console.request.ChannelRequest;
import com.cjk.console.service.ChannelService;
import com.cjk.foundation.commons.response.PageResponse;
import com.cjk.foundation.commons.response.Response;

@Controller
@RequestMapping("/channel")
public class ChannelController {
	
	@Autowired
	private ChannelService channelService;
	
	@ResponseBody
	@RequestMapping(value = "/addChannel.do", method = RequestMethod.POST)
	public Response addChannel(@RequestBody ChannelRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return channelService.addChannel(request, userId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getChannelList.do", method = RequestMethod.POST)
	public PageResponse getChannelList(@RequestBody ChannelRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return channelService.getChannelList(request, userId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getChannelDetailList.do", method = RequestMethod.POST)
	public Response getChannelDetailList(@RequestBody ChannelRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return channelService.getChannelDetailList(request, userId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/editChannel.do", method = RequestMethod.POST)
	public Response editChannel(@RequestBody ChannelRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return channelService.editChannel(request, userId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/delChannel.do", method = RequestMethod.POST)
	public Response delChannel(@RequestBody ChannelRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return channelService.delChannel(request, userId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/delChannelDetail.do", method = RequestMethod.POST)
	public Response delChannelDetail(@RequestBody ChannelRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return channelService.delChannelDetail(request, userId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/confChannel.do", method = RequestMethod.POST)
	public Response confChannel(@RequestBody ChannelRequest request, HttpSession session) {
		final Integer userId = SessionApi.getLoginUserId(session);
		return channelService.confChannel(request, userId);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getChannel.do", method = RequestMethod.POST)
	public Response getChannel(@RequestBody ChannelRequest request) {
		return channelService.getChannel(request.getId());
	}
	
	@RequestMapping(value = "/{userId}/{groupId}/{fileName:.+}", method = RequestMethod.GET)
	public ResponseEntity<byte[]> download(@PathVariable("userId") Integer userId, @PathVariable("groupId") Integer groupId, @PathVariable("fileName") String fileName) {
		String filePath = "/easyTv/data/channel/user_" + userId + "/group_" + groupId + "/";
		File adDataDirectory = new File(filePath);
		File file = new File(adDataDirectory.getAbsoluteFile(), fileName);
		byte[] a = null;
		try {
			a = FileUtils.readFileToByteArray(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.add("Content-Disposition", "attachment;filename=" + fileName);
		return new ResponseEntity<>(a, headers, HttpStatus.OK);
	}
	
}
